__
Support Vector Machines Project¶
The Data¶
For this series of lectures, we will be using the famous Iris flower data set.
The Iris flower data set or Fisher's Iris data set is a multivariate data set introduced by Sir Ronald Fisher in the 1936 as an example of discriminant analysis.
The data set consists of 50 samples from each of three species of Iris (Iris setosa, Iris virginica and Iris versicolor), so 150 total samples. Four features were measured from each sample: the length and the width of the sepals and petals, in centimeters.
Here's a picture of the three different Iris types:
# The Iris Setosa
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/5/56/Kosaciec_szczecinkowaty_Iris_setosa.jpg'
Image(url,width=300, height=300)
# The Iris Versicolor
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/4/41/Iris_versicolor_3.jpg'
Image(url,width=300, height=300)
# The Iris Virginica
from IPython.display import Image
url = 'http://upload.wikimedia.org/wikipedia/commons/9/9f/Iris_virginica.jpg'
Image(url,width=300, height=300)
The iris dataset contains measurements for 150 iris flowers from three different species.
The three classes in the Iris dataset:
Iris-setosa (n=50)
Iris-versicolor (n=50)
Iris-virginica (n=50)
The four features of the Iris dataset:
sepal length in cm
sepal width in cm
petal length in cm
petal width in cm
Get the data¶
Use seaborn to get the iris data by using: iris = sns.load_dataset('iris')
import seaborn as sns
iris = sns.load_dataset('iris')
iris.head()
| sepal_length | sepal_width | petal_length | petal_width | species | |
|---|---|---|---|---|---|
| 0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
| 1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
| 2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
| 3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
| 4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
Let's visualize the data and get you started!
Exploratory Data Analysis¶
Time to put your data viz skills to the test! Try to recreate the following plots, make sure to import the libraries you'll need!
Import some libraries you think you'll need.
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
** Create a pairplot of the data set. Which flower species seems to be the most separable?**
# Setosa is the most separable.
sns.pairplot(iris,hue='species',palette='Dark2')
<seaborn.axisgrid.PairGrid at 0x1bd77efdcd0>
Train Test Split¶
** Split your data into a training set and a testing set.**
from sklearn.model_selection import train_test_split
X = iris.drop('species',axis=1)
y = iris['species']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)
Train a Model¶
Now its time to train a Support Vector Machine Classifier.
Call the SVC() model from sklearn and fit the model to the training data.
from sklearn.svm import SVC
svc_model = SVC()
SVC()
svc_model.fit(X_train,y_train)
SVC()
Model Evaluation¶
Now get predictions from the model and create a confusion matrix and a classification report.
predictions = svc_model.predict(X_test)
from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,predictions))
[[18 0 0] [ 0 13 0] [ 0 0 14]]
print(classification_report(y_test,predictions))
precision recall f1-score support
setosa 1.00 1.00 1.00 18
versicolor 1.00 1.00 1.00 13
virginica 1.00 1.00 1.00 14
accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45
svc_model1 = SVC(kernel='linear')
SV
svc_model1.fit(X_train,y_train)
SVC(kernel='linear')
predictions1 = svc_model1.predict(X_test)
from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,predictions1))
[[18 0 0] [ 0 13 0] [ 0 0 14]]
print(classification_report(y_test,predictions1))
precision recall f1-score support
setosa 1.00 1.00 1.00 18
versicolor 1.00 1.00 1.00 13
virginica 1.00 1.00 1.00 14
accuracy 1.00 45
macro avg 1.00 1.00 1.00 45
weighted avg 1.00 1.00 1.00 45
svc_model2 = SVC(kernel = 'sigmoid')
svc_model2.fit(X_train,y_train)
SVC(kernel='sigmoid')
predictions2 = svc_model2.predict(X_test)
from sklearn.metrics import classification_report,confusion_matrix
print(confusion_matrix(y_test,predictions2))
print(classification_report(y_test,predictions2))
[[ 0 18 0]
[ 0 13 0]
[ 0 14 0]]
precision recall f1-score support
setosa 0.00 0.00 0.00 18
versicolor 0.29 1.00 0.45 13
virginica 0.00 0.00 0.00 14
accuracy 0.29 45
macro avg 0.10 0.33 0.15 45
weighted avg 0.08 0.29 0.13 45
C:\ProgramData\Anaconda3\lib\site-packages\sklearn\metrics\_classification.py:1221: UndefinedMetricWarning: Precision and F-score are ill-defined and being set to 0.0 in labels with no predicted samples. Use `zero_division` parameter to control this behavior. _warn_prf(average, modifier, msg_start, len(result))